<template>
	<view>
		<view class="change_pwd_box">
			<view class="items">
				<text>新密码</text>
				<input type="password" placeholder="请输入新密码" v-model="new_pwd">
			</view>
			<view class="items">
				<text>确认新密码</text>
				<input type="password" placeholder="请输入新密码" v-model="new_pwds">
			</view>
			<view class="items">
				<text>手机验证码</text>
				<view class="yzm_box">
					<input type="safe-password" placeholder="请输入手机验证码" placeholder-style="color:#bebfc3" v-model="code">
					<view class="codeBtn" @click="getCode()">
						<text>{{codetext}}</text>
					</view>
				</view>
			</view>
			<view class="msg">
				<text>验证码将发送到您注册的手机号【{{phone}}】上！</text>
			</view>
		</view>
		<view class="order" @click="_order">确认</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				new_pwd: '',
				new_pwds: '',
				code: '',
				codetext: '获取验证码',
				isDisabled: true,
				userinfo: {},
				phone: ''
			}
		},
		onLoad() {
			this.userinfo = uni.getStorageSync('userinfo');
			this.phone = this.userinfo.phone.substring(0,3) + '****' + this.userinfo.phone.substring(7)
		},
		methods: {
			async _order() {
				if (this.new_pwd == '' || this.new_pwd == null) {
					uni.showToast({
						title: '请输入新密码',
						icon: 'none',
						duration: 3000
					})
					return false
				}
				if (this.new_pwds == '' || this.new_pwds == null) {
					uni.showToast({
						title: '请再次输入新密码',
						icon: 'none',
						duration: 3000
					})
					return false
				}
				if (this.new_pwd != this.new_pwds) {
					uni.showToast({
						title: '两次输入的密码不一致',
						icon: 'none',
						duration: 3000
					})
					return false
				}
				if (this.code == '' || this.code == null) {
					uni.showToast({
						title: '请输入验证码',
						icon: 'none',
						duration: 3000
					})
					return false
				}
				uni.showLoading({
					title: '加载中'
				});
				const {
					data: res
				} = await this.$myHttp({
					url: 'user/updatePwds',
					data: {
						new_pwd: this.new_pwd,
						new_pwds: this.new_pwds,
						code: this.code,
						phone: this.userinfo.phone
					},
				});
				if (res.code == 200) {
					uni.showToast({
						title: res.msg,
						duration: 2000,
						icon: 'none',
						success() {
							setTimeout(function() {
								uni.removeStorageSync('userinfo');
								uni.reLaunch({
									url: '/pages/login/login'
								})
							}, 2000)
						}
					});
				} else {
					uni.showToast({
						title: res.msg,
						icon: 'none',
						duration: 3000
					})
				}
			},
			//获取验证码
			getCode() {
				uni.hideKeyboard() //隐藏已经显示的软键盘，如果软键盘没有显示则不做任何操作。
				let that = this;
				// 防止多次重复点击
				if (!that.isDisabled) {
					return false;
				}
				if (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(this.userinfo.phone))) { //校验手机号码是否有误
					uni.showToast({
						title: '请填写正确手机号码',
						icon: "none"
					});
					return false;
				}
				this.codetext = "发送中..." //发送验证码
				this.isDisabled = false;
				//示例用定时器模拟请求效果
				//setTimeout(()用于在指定的毫秒数后调用函数或计算表达式
				setTimeout(() => {
					this.setTimer();
				}, 1000)
			},
			// 验证码的定时器
			setTimer() {
				// 发送请求
				this.V_Code()
			},
			// 获取验证码请求
			async V_Code() {
				const {
					data: res
				} = await this.$myHttp({
					url: 'v1/send',
					data: {
						mobile: this.userinfo.phone,
						event: 'uppwd'
					},
				});
				if (res.code == 200) {
					uni.showToast({
						title: res.msg,
						icon: "none"
					});
					clearInterval(this.getCode());
					let holdTime = 60; //定义变量并赋值
					this.codetext = "重新获取(60)"
					//setInterval（）是一个实现定时调用的函数，可按照指定的周期（以毫秒计）来调用函数或计算表达式。
					//setInterval方法会不停地调用函数，直到 clearInterval被调用或窗口被关闭。
					this.Timer = setInterval(() => {
						if (holdTime <= 0) {
							this.isDisabled = true;
							this.getCodeBtnColor = "#ffffff";
							this.codetext = "获取验证码"
							clearInterval(this.Timer); //清除该函数
							return; //返回前面
						}
						this.codetext = "重新获取(" + holdTime + ")"
						holdTime--;
					}, 1000)
				} else {
					this.isDisabled = true;
					this.getCodeBtnColor = "#ffffff";
					this.codetext = "获取验证码"
					uni.showToast({
						title: res.msg,
						duration: 2000,
						icon: "none"
					});
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.change_pwd_box {
		margin: 30rpx;
		padding: 30rpx;
		background-color: #fff;
		box-sizing: border-box;
		
		.items {
			margin-top: 40rpx;
			
			text {
				font-size: 28rpx;
				color: #333;
				padding-left: 10rpx;
				margin: 20rpx 0;
				display: block;
			}
			
			input {
				padding: 30rpx;
				border: #eeeeee 1px solid;
				border-radius: 10rpx;
				font-size: 28rpx;
			}
			
			.yzm_box {
				display: flex;
				align-items: center;
				justify-content: space-between;
			
				input {
					width: 50%;
				}
			
				.codeBtn {
					border: 1px solid #2979ff;
					border-radius: 8rpx;
					padding: 10rpx 30rpx;
			
					text {
						color: #2979ff;
						font-size: 28rpx;
						display: block;
						margin-right: 15rpx;
					}
				}
			}
		}
		
		.msg {
			font-size: 24rpx;
			color: #fa3534;
			padding-top: 20rpx;
		}
	}

	.order {
		background-color: #1296db;
		margin: 50rpx 20rpx;
		text-align: center;
		padding: 30rpx 0;
		border-radius: 10rpx;
		color: #fff;
		transition: all 0.5s;
	}

	.order:active {
		background-color: #1296db;
	}
</style>
<style>
	.placeholder-class {
		color: #e1e1e1;
	}
</style>
<style scoped lang="scss">
	.placeholder-class {
		color: #e1e1e1;
	}
</style>